//
//  XSRegistViewController.m
//  MeiLiYiChu
//
//  Created by mac on 16/2/29.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "XSRegistViewController.h"

@interface XSRegistViewController ()
{
    UITextField  *userTF;
    NSTimer * timer;
    UIButton * button;
    UITextField  *pass;
    UITextField  *test;
}
@end

@implementation XSRegistViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    XSView * view =[[XSView alloc]initWithBool:NO withString:@"注册"];
    [self.view addSubview:view];
    UIButton * btn1 =(UIButton *)[view viewWithTag:1];
    [btn1 addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    
    self.view.backgroundColor =[UIColor whiteColor];
    
    UIView * userView =[[UIView alloc]init];
    
    userView.backgroundColor =[UIColor grayColor];
    [self.view addSubview:userView];
    [userView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(104);
        make.left.offset(10);
        make.right.offset(-10);
        make.height.equalTo(@50);
        
        
    }];
    
    userTF =[[UITextField alloc]init];
    userTF.placeholder =@"手机号";
    userTF.backgroundColor =[UIColor lightTextColor];
    [userView addSubview:userTF];
    [userTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.offset(50);
        make.right.offset(0);
        make.bottom.offset(0);
    }];
    
    UIImageView * imageView =[[UIImageView alloc]init];
    imageView.image =[UIImage imageNamed:@"login_user_icon"];
    imageView.frame =CGRectMake(0, 0, 50, 50);
    imageView.backgroundColor =[UIColor grayColor];
    [userView addSubview:imageView];
    
    
    UIView * passView =[[UIView alloc]init];
    
    passView.backgroundColor =[UIColor grayColor];
    [self.view addSubview:passView];
    [passView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(userView.mas_bottom).with.offset(20);
        make.left.offset(10);
        make.right.offset(-10);
        make.height.equalTo(@50);
    }];
    
    pass =[[UITextField alloc]init];
    pass.placeholder =@"密码";
    pass.backgroundColor =[UIColor lightTextColor];
    [passView addSubview:pass];
    
    [pass mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.offset(50);
        make.right.offset(0);
        make.bottom.offset(0);
    }];
    
    UIImageView * imageView1 =[[UIImageView alloc]init];
    imageView1.image =[UIImage imageNamed:@"login_pass_icon"];
    imageView1.frame =CGRectMake(0, 0, 50, 50);
    [passView addSubview:imageView1];

    
    UIView * testView =[[UIView alloc]init];
    
    testView.backgroundColor =[UIColor grayColor];
    [self.view addSubview:testView];
    [testView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(passView.mas_bottom).with.offset(20);
        make.left.offset(10);
        make.right.offset(-10);
        make.height.equalTo(@50);
    }];
    
    test =[[UITextField alloc]init];
    test.placeholder =@"验证码";
    test.backgroundColor =[UIColor lightTextColor];
    [testView addSubview:test];
    
    [test mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(0);
        make.left.offset(50);
        make.right.offset(0);
        make.height.equalTo(@50);
        make.bottom.offset(0);
    }];
    
    UIImageView * imageView2 =[[UIImageView alloc]init];
    imageView2.image =[UIImage imageNamed:@"user_serviec"];
    imageView2.frame =CGRectMake(0, 0, 50, 50);
    [testView addSubview:imageView2];

    button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.backgroundColor = [UIColor magentaColor];
    [button setTitle:@"获取验证码" forState:UIControlStateNormal];
    //获取验证码状态
    button.selected =NO;
    [button addTarget:self action:@selector(sendTest:) forControlEvents:UIControlEventTouchUpInside];
    [testView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(0);
        make.top.offset(0);
        make.bottom.offset(0);
        make.width.equalTo(@100);
    }];

    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeCustom];
    
    button1.center =CGPointMake(self.view.center.x, self.view.center.y+80);
    button1.bounds =CGRectMake(0, 0, 250, 40);
    
    
    [button1 setTitle:@"注册" forState:UIControlStateNormal];
    button1.backgroundColor =[UIColor magentaColor];
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [button1 addTarget:self action:@selector(registBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    
}
-(void)back
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
-(void)sendTest:(UIButton *)btn
{
    //判断输入手机号符不符合规则
    NSString *strRegex = @"^1(3[0-9]|5[0-35-9]|8[025-9])\\d{8}$";

    NSPredicate * predicate =[NSPredicate predicateWithFormat:@"self matches %@",strRegex];
    BOOL isPhone =[predicate evaluateWithObject:userTF.text];
    if (isPhone) {
        NSLog(@"是手机格式");
        
        
        [XSHTTPManager getTestWithComplection:^(NSString *isRegister) {
            if ([isRegister isEqualToString:@"true"]) {
                UIAlertController * alert =[UIAlertController alertControllerWithTitle:@"温馨提示!" message:@"当前用户已注册" preferredStyle:UIAlertControllerStyleAlert];
                UIAlertAction * btn =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
                [alert addAction:btn];
                [self presentViewController:alert animated:YES completion:nil];
            }
            else
            {
                //开始注册
                timer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(changeTest) userInfo:nil repeats:YES];
                //添加到当前事件循环
                [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
                //按钮不能执行
                btn.enabled =NO;

            }
            
        } withUser:userTF.text psw:pass.text ];

//       
//        [SMSSDK getVerificationCodeByMethod:SMSGetCodeMethodSMS phoneNumber:userTF.text
//                                       zone:@"86"
//                           customIdentifier:nil
//                                     result:^(NSError *error){
//                                         if (!error)
//                                         {
//                                             NSLog(@"获取验证码成功");
//                                         } else
//                                         {
//                                             NSLog(@"错误信息：%@",error);
//                                         }}];
        
    }
    else
    {
        UIAlertController * alert =[UIAlertController alertControllerWithTitle:@"温馨提示!" message:@"请输入正确的手机号" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * btn =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert addAction:btn];
        [self presentViewController:alert animated:YES completion:nil];
    }
}
-(void)changeTest
{
    static int i =3;
    i--;
    
    button.selected =YES;
    button.backgroundColor =[UIColor grayColor];
    button.titleLabel.font =[UIFont systemFontOfSize:13];
    [button setTitle:[NSString stringWithFormat:@"%d秒",i] forState:UIControlStateNormal];
//    button.titleLabel.text =[NSString stringWithFormat:@"%d秒",i];
    
    if (i==0) {
        [timer invalidate];
        timer=nil;
        
        button.selected =NO;
        button.backgroundColor =[UIColor magentaColor];
        [button setTitle:@"重新发送" forState:UIControlStateNormal];
        button.enabled = YES;
        i =3;
    }
}
-(void)registBtn
{
    
//    [SMSSDK commitVerificationCode:self.verifyCodeField.text phoneNumber:userTF zone:_areaCode result:^(NSError *error) {
//        
//        if (!error) {
//            NSLog(@"验证成功");
//        }
//        else
//        {
//            NSLog(@"错误信息:%@",error);
//        }
//    }];
    [XSHTTPManager getRegisterPlistWithComplection:^(NSString * string) {
        if ([string isEqualToString:@"ok"]) {
            [self.navigationController popToRootViewControllerAnimated:YES];
        }
        else
        {
            UIAlertController * alert =[UIAlertController alertControllerWithTitle:@"温馨提示!" message:@"手机号已注册" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction * btn =[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
            [alert addAction:btn];
            [self presentViewController:alert animated:YES completion:nil];
        }
        
    } withUserID:userTF.text Withpsw:pass.text :test.text];
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
